package com.easylinkin.linkappapi.devicemanager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.devicemanager.entity.DeviceUnit;
import com.easylinkin.linkappapi.devicemanager.entity.WireropeEntity;
import com.easylinkin.linkappapi.devicemanager.entity.WireropeTitleEntity;
import com.easylinkin.linkappapi.devicemanager.mapper.WireropeMapper;
import com.easylinkin.linkappapi.devicemanager.service.WireropeService;
import com.easylinkin.linkappapi.devicemanager.vo.WireropeVo;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author linWenLe
 * @Date: 2024/3/28
 * @Description: 钢丝绳
 */
@Slf4j
@Service("wireropeService")
public class WireropeServiceImpl extends ServiceImpl<WireropeMapper, WireropeEntity> implements WireropeService {

    private static final String WIREROPE_DEVICE_CODE = "235c30efecb511ee9f580242ac110009";

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Resource
    private WireropeMapper wireropeMapper;

    @Override
    public WireropeTitleEntity getWireropeInfo(String deviceId) {
        WireropeTitleEntity entity = wireropeMapper.getWireropeInfo(deviceId);
        JSONObject jsonObject = JSONObject.parseObject(entity.getData());
        if (jsonObject != null) {
            entity.setInterval(jsonObject.getString("interval"));
            entity.setStatus(jsonObject.getInteger("status"));
            entity.setStatusRemark(jsonObject.getString("status_remark"));
        }
        entity.setAlertMode(wireropeMapper.getAlertMode(deviceId));
        return entity;
    }

    @Override
    public IPage<WireropeEntity> getHistoryAlertList(RequestModel<WireropeVo> restPage) {
        Page page = restPage.getPage();
        WireropeVo model = restPage.getCustomQueryParams();
        IPage<WireropeEntity> list =wireropeMapper.getHistoryAlertList(page, model);
        return list;
    }

    @Override
    public List<DeviceUnit> deviceList() {
        String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        return wireropeMapper.deviceList(WIREROPE_DEVICE_CODE, tenantId);
    }

    /**
     * 最大展示期限7天的数据
     * @param entity
     * @return
     */
    @Override
    public Map<String,Object> getWireropeList(WireropeVo entity) {
        Assert.notNull(entity.getDeviceId(),"设备id不能为空");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long start = 0;
        try {
            start = format.parse(entity.getStartTime()).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
//        if (StringUtils.isEmpty(entity.getStartTime())) {
//            start = System.currentTimeMillis() - 2 * 24 * 60 * 60 * 1000;
//        }else {
//            try {
//                start = format.parse(entity.getStartTime()).getTime();
//                long sevenDayBefore = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000;
//                if (start < sevenDayBefore) {
//                    start = sevenDayBefore;
//                }
//            } catch (ParseException e) {
//                throw new RuntimeException(e);
//            }
//        }

        if (StringUtils.isEmpty(entity.getEndTime())) {
            entity.setEndTime(format.format(new Date()));
        }
        entity.setStartTime(format.format(new Date(start)));
        List<Date> timeList = new ArrayList<>();
        List<String> resultList = new ArrayList<>();

        List<WireropeEntity> list =wireropeMapper.getWireropeList(entity);
        list.forEach(e -> {
            timeList.add(e.getTime());
            resultList.add(e.getResult());
        });
        Map<String,Object> map = new HashMap<>();
        map.put("timeList",timeList);
        map.put("resultList",resultList);
        return map;
    }

}
